﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace RBAC.Administry.Account
{
    public partial class Actor : System.Web.UI.Page
    {
        IList<RBAC.Role> _roles;
        /// <summary>
        /// 获得当前请求的账户所指派的角色清单
        /// </summary>
        /// <returns></returns>
        protected IList<RBAC.Role> GetAssignedRoles()
        {
            if (_roles == null)
            {
                RBAC.Account account = ParseRequest();
                _roles = account.GetRoles().ToList();
            }
            return _roles;
        }

        #region page logic

        /// <summary>
        /// 载入数据
        /// </summary>
        private void LoadData()
        {
            RBAC.Account account = ParseRequest();

            if (account != null)
            {
                // 绑定账户信息
                this.account.Text = account.ID;
                this.display.Text = string.IsNullOrEmpty(account.Display) ? "无" : account.Display;

                BindRoleGrid();
            }
            else
            {
                report.ReportFailure("请求的账户不存在!");
            }
        }
        /// <summary>
        /// 绑定角色清单
        /// </summary>
        private void BindRoleGrid()
        {
            grid.DataSource = RBAC.Role.LoadAll();
            grid.DataBind();
        }
        /// <summary>
        /// 解析用户请求的账户
        /// </summary>
        private RBAC.Account ParseRequest()
        {
            string id = Request.QueryString["account"];
            if (!string.IsNullOrEmpty(id))
            {
                return RBAC.Account.Load(id);
            }
            return null;
        }
        /// <summary>
        /// 在绑定行时调用，指出是否应该选中“指派”选框
        /// </summary>
        /// <param name="dataItem">绑定到该行的Role实例</param>
        protected bool IsAssign(object dataItem)
        {
            return GetAssignedRoles().Contains((RBAC.Role)dataItem);
        }
        /// <summary>
        /// 解除角色指派
        /// </summary>
        private void Unassign(string role)
        {
            RBAC.Account account = ParseRequest();
            account.Unassign(role);
            account.Save();

            PageHelper.Script(this, "unassigned();",scriptUpdatePanel);
        }

        /// <summary>
        /// 指派角色
        /// </summary>
        /// <param name="role"></param>
        private void Assign(string role)
        {
            RBAC.Account account = ParseRequest();
            account.Assign(role);
            account.Save();

            PageHelper.Script(this, "assigned();", scriptUpdatePanel);
        }

        #endregion

        #region event processing

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        protected void assign_Changed(object sender, EventArgs e)
        {
            CheckBox assign = sender as CheckBox;
            GridViewRow row = assign.NamingContainer as GridViewRow;
            string key = grid.DataKeys[row.RowIndex].Value.ToString();

            if (assign.Checked)
            {
                Assign(key);
            }
            else
            {
                Unassign(key);
            }
        }

        protected void grid_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grid.PageIndex = e.NewPageIndex;
            BindRoleGrid();
        }

        #endregion


    }
}
